//
//  HomeNewView.h
//  LipuWaterPurifier
//
//  Created by Leon He on 2017/5/17.
//  Copyright © 2017年 Lipu. All rights reserved.
//

#import "PagingScrollView.h"

#define NavTabHeight            0
//页面除去导航和底部Tab后的高度
#define PageContentHeight       (SCREEN_HEIGHT - NavTabHeight)

#define DownPagerTabHeight      50
//除去顶部tab后的内容高度
#define DownContentHeight       PageContentHeight-DownPagerTabHeight
#define WaveBelowOffset         15

@interface PagingScrollView()<UIScrollViewDelegate>
{
    int currentTab;
    int currentScrollPage;//0:up 1:down
    CGRect arrowOriginRect;
    CGRect downOriginRect;
    int innerScollStatus;//0:top 1:down 2:bettween
}

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIView *tabView;
@property (nonatomic, strong) UIView *arrowView;
@property (nonatomic, strong) UIView *downViewContainer;
@property (nonatomic, strong) UIView *tdsView;
@property (nonatomic, strong) UIScrollView *recoScrollview;
@property (nonatomic, strong) UIView *filterView;

@end

@implementation PagingScrollView
@synthesize mainScrollView, tabView, arrowView, tdsView, filterView, recoScrollview, downViewContainer;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    NSLog(@"SCREEN_HEIGHT = %f, PageContentHeight = %f", SCREEN_HEIGHT, PageContentHeight);
    
    mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (PageContentHeight)/2.0)];//高度为屏幕一半
    mainScrollView.backgroundColor = [UIColor greenColor];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.clipsToBounds = NO;
    //    mainScrollView.delaysContentTouches = NO;
    mainScrollView.delegate = self;
    [self addSubview:mainScrollView];
    mainScrollView.tag = 2000;
    
    tabView = [[UIView alloc]initWithFrame:CGRectMake(0, (PageContentHeight)/2.0 , SCREEN_WIDTH, (PageContentHeight)/4.0)];//实际不是这个高度
    tabView.backgroundColor = [UIColor yellowColor];
    [mainScrollView addSubview:tabView];
    tabView.tag = 2003;
    
    for(int i = 0; i < 3; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH)/3 * i, 0, (SCREEN_WIDTH)/3, (PageContentHeight)/4.0)];
        [button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        button.tag = i;
        [button addTarget:self action:@selector(onTabClick:) forControlEvents:UIControlEventTouchUpInside];
        [tabView addSubview:button];
    }
    
    arrowOriginRect = CGRectMake(0, (PageContentHeight) * 3.0 /4.0 , SCREEN_WIDTH, (PageContentHeight)/4.0);
    arrowView = [[UIView alloc]initWithFrame:arrowOriginRect];
    arrowView.backgroundColor = [UIColor purpleColor];
    [mainScrollView addSubview:arrowView];
    arrowView.tag = 2004;
    
    downOriginRect = CGRectMake(0, (PageContentHeight) , SCREEN_WIDTH, (PageContentHeight) * 3.0 /4.0);
    downViewContainer = [[UIView alloc]initWithFrame:downOriginRect];
    [mainScrollView addSubview:downViewContainer];
    [mainScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, (PageContentHeight))];//高度为内容的一半
    downViewContainer.tag = 2005;
    
    tdsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (PageContentHeight) * 3.0 /4.0)];
    tdsView.backgroundColor = [UIColor orangeColor];
    [downViewContainer addSubview:tdsView];
    currentTab = 0;
    
    recoScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (PageContentHeight) * 3.0 /4.0)];
    recoScrollview.backgroundColor = [UIColor grayColor];
    recoScrollview.bounces = NO;
    [downViewContainer addSubview:recoScrollview];
    recoScrollview.tag = 2006;
    //
    UIView *recoInnerView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, (PageContentHeight) * 3.0 /4.0)];
    recoInnerView1.backgroundColor = [UIColor blueColor];
    UIView *recoInnerView2 = [[UIView alloc]initWithFrame:CGRectMake(0, (PageContentHeight) * 3.0 /4.0 , SCREEN_WIDTH, (PageContentHeight) * 3.0 /4.0)];
    recoInnerView2.backgroundColor = [UIColor redColor];
    [recoScrollview addSubview:recoInnerView1];
    [recoScrollview addSubview:recoInnerView2];
    recoScrollview.contentSize = CGSizeMake(SCREEN_WIDTH, (PageContentHeight)*3.0/2);
    recoInnerView1.tag = 2007;
    recoInnerView2.tag = 2008;
    
    filterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (PageContentHeight) * 3.0 /4.0)];
    filterView.backgroundColor = [UIColor blueColor];
    filterView.tag = 2009;
    
}

#pragma mark - 事件响应处理
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSLog(@"\n=======================\n");
    NSLog(@"hitTest point:(%f, %f)", point.x, point.y);
    //把当前的点转换成mainScrollView控件坐标系上的点.
    CGPoint scrollPoint = [mainScrollView convertPoint:point toView:mainScrollView];
    scrollPoint.y += mainScrollView.contentOffset.y;
    NSLog(@"\n scrollPoint in scrollview: (%f, %f)", scrollPoint.x, scrollPoint.y);
    UIView *targetView;
    for(UIView *scrollSubview in mainScrollView.subviews){
        
        NSLog(@"subview in mainScrollView: %@", scrollSubview);
        //把当前的点转换成mainScrollView的子控件坐标系上的点.
        CGPoint scrollSubPoint = [mainScrollView convertPoint:scrollPoint toView:scrollSubview];
        NSLog(@"scrollSubPoint:(%f, %f)", scrollSubPoint.x, scrollSubPoint.y);
        targetView = [scrollSubview hitTest:scrollSubPoint withEvent:event];
        if(targetView){
            NSLog(@"hitTest find target subView:%@", targetView);
            return targetView;
        }
    }
    
    NSLog(@"hitTest mainScrollView");
    return mainScrollView;
}

- (void)onTabClick:(UIButton*)sender{
    NSInteger index = sender.tag;
    switch (index) {
        case 0:
            if(currentTab != 0){
                [self removeDownSubview];
                [downViewContainer addSubview:tdsView];
            }
            currentTab = 0;
            break;
        case 1:
            if(currentTab != 1){
                [self removeDownSubview];
                [downViewContainer addSubview:recoScrollview];
            }
            currentTab = 1;
            break;
        case 2:
            if(currentTab != 2){
                [self removeDownSubview];
                [downViewContainer addSubview:filterView];
            }
            currentTab = 2;
            break;
        case 4:
            NSLog(@"you click me!");
            break;
        default:
            break;
    }
}

- (void)removeDownSubview{
    if(currentTab == 0){
        [tdsView removeFromSuperview];
    } else if(currentTab == 1){
        [recoScrollview removeFromSuperview];
    } else {
        [filterView removeFromSuperview];
    }
}

float lastOuterOffsetY;
float lastInnerOffsetY;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    float offsetY = scrollView.contentOffset.y;
    
    //    if(scrollView.tag == 2001){
    
    NSLog(@"scrollViewDidScroll offsetY = %f", offsetY);
    
    if(offsetY >= lastOuterOffsetY){//往上滑
        if(0 <= offsetY && offsetY <= PageContentHeight){//0->PageContentHeight
            CGRect arrowRect = arrowOriginRect;
            arrowRect.size.height -= offsetY/2;
            arrowView.frame = arrowRect;
            
            CGRect tdsRect = downOriginRect;
            tdsRect.origin.y -= offsetY/2;
            downViewContainer.frame = tdsRect;
        }
    }
    
    if(offsetY < lastOuterOffsetY){//往下滑
        if(offsetY >= 0 && offsetY <= PageContentHeight){//PageContentHeight->0
            CGRect arrowRect = arrowOriginRect;
            arrowRect.size.height = (PageContentHeight)/2.0 - offsetY/2;
            arrowView.frame = arrowRect;
            
            CGRect tdsRect = downOriginRect;
            tdsRect.origin.y += -offsetY/2;
            downViewContainer.frame = tdsRect;
        }
    }
    lastOuterOffsetY = offsetY;
    //    }
    //    else if(scrollView.tag == 2006){
    //        if(offsetY == 0){
    //            innerScollStatus = 0;
    //        } else if(scrollView.contentSize.height == offsetY){
    //            innerScollStatus = 1;
    //        } else {
    //            innerScollStatus = 2;
    //        }
    //    }
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    float offsetY = scrollView.contentOffset.y;
//    NSLog(@"scrollViewDidEndDecelerating, offsetY = %f", offsetY);
//    if(offsetY == 0){
//
//        [self scrollTop];
//
//    } else if(offsetY == PageContentHeight){
//
//        [self scrollDown];
//
//    }
//}
//
//- (void)scrollTop{
//    dispatch_async(dispatch_get_main_queue(), ^{
//
//        [UIView animateWithDuration:0.1 animations:^{
//            arrowView.frame = arrowOriginRect;
//            downViewContainer.frame = downOriginRect;
//        }];
//
//    });
//}
//
//- (void)scrollDown{
//    dispatch_async(dispatch_get_main_queue(), ^{
//
//        [UIView animateWithDuration:0.1 animations:^{
//            CGRect arrowRect = arrowOriginRect;
//            arrowRect.size.height = 0;
//            arrowView.frame = arrowRect;
//
//            CGRect tdsRect = downOriginRect;
//            tdsRect.origin.y -= (PageContentHeight)/2.0;
//            downViewContainer.frame = tdsRect;
//        }];
//
//    });
//}

@end

